package cn.com.tom.dao.impl;

import cn.com.tom.bean.Skill;
import cn.com.tom.dao.SkillDao;
import cn.com.tom.dao.impl.BaseDaoImpl;
import cn.com.tom.util.Dbs;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;

public class SkillDaoImpl extends BaseDaoImpl<Skill> implements SkillDao {

    @Override
    public boolean save(Skill bean) {
        Integer id = bean.id;
        String sql = "";
        List<Object> args = new ArrayList<>();
        args.add(bean.getName());
        args.add(bean.getLevel());
        if (id == null || id < 1) {
            sql = "INSERT INTO skill(name,level) VALUES(?,?)";
        } else {
            sql = "UPDATE skill SET name=?,level=? WHERE id=?";
            args.add(id);
        }

        System.out.println("sql = " + sql);
        return Dbs.save(sql, args.toArray());
    }

    @Override
    public Skill get(Integer id) {
        return null;
    }

    @Override
    public List<Skill> list() {
        String sql = "SELECT id,created_time,name,level FROM skill";
        return Dbs.list(sql, new BeanPropertyRowMapper<>(Skill.class));
    }
}
